/**
 *
 * The MIT License
 *
 * Copyright (c) 2008 Board of Trustees, Leland Stanford Jr. University. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.macdadi.multi.client.components;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import org.macdadi.core.client.domain.DataService;
import org.macdadi.core.client.domain.NewsItem;
import org.macdadi.multi.client.Overview;

/**
 * @author Ben Suter <ben.suter@gmail.com>
 */
public class NewsPanel extends Composite
{
    public static final String CSS_CLASS = "macdadi-NewsPanel";

    protected FlexTable table;

    public NewsPanel()
    {
        VerticalPanel vp = new VerticalPanel();
        vp.add(new HTML("<h4>News</h4>"));

        table = new FlexTable();
        vp.add(table);

        initWidget(vp);
        setStyleName(CSS_CLASS);
    }

    protected void onLoad()
    {
        DataService.App.getInstance().listNewsItems(Overview.getCurrentAuthToken(), new AsyncCallback()
        {
            public void onFailure(Throwable caught)
            {
                GWT.log("Failed to populate the news table", caught);
            }

            public void onSuccess(Object result)
            {
                NewsItem[] news = (NewsItem[]) result;

                NewsItem n;
                for (int i = 0; i < news.length; i++ ) {
                    n = news[i];
                    addItemToTable(n.getPubDateStr(), n.getAuthor(), n.getText());
                }
            }
        });
    }

    protected void addItemToTable(String date, String author, String message) {
        int row = table.getRowCount();

        FlexTable.FlexCellFormatter formatter = table.getFlexCellFormatter();

        table.getRowFormatter().setVerticalAlign(row, HasAlignment.ALIGN_TOP);

        table.setWidget(row, 0, new HTML(message));
        formatter.setColSpan(row, 0, 2);

        row++;
        
        table.setWidget(row, 0, new HTML(date + " by " + author));
        formatter.setWordWrap(row, 0, false);
        formatter.setHorizontalAlignment(row, 0, HasAlignment.ALIGN_RIGHT);
        formatter.setVerticalAlignment(row, 0, HasAlignment.ALIGN_TOP);
        formatter.setHeight(row, 0, "35px");
    }
}
